package com.beyondsoft.mcms.domain;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findDocumentsByConference" })
public class Document {

    /**
     */
    @NotNull
    @Size(max = 30,min=1)
    @Column(unique = true)
    private String name;

    /**
     */
    @NotNull
    @Size(max = 300)
    private String description;

    /**
     */
    private String fileName;

    /**
     */
    @NotNull
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private byte[] content;

    /**
     */
    private String contentType;

    /**
     */
    private Long fileSize;

    /**
     */
    @Size(max = 100)
    @Transient
    private String url;

    /**
     */
    @ManyToOne
    private Conference conference;
    
    /**
     */
    @NotNull
    private String createdBy;

    /**
     */
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "SS")
    private Date createDate;

    public static TypedQuery<Document> findDocumentsByConference(Conference conference) {
        
    	EntityManager em = Document.entityManager();
    	if (conference == null) {
    		TypedQuery<Document> q = em.createQuery("SELECT o FROM Document AS o order by o.conference.id ", Document.class);
            return q;
        }
        else{
	        TypedQuery<Document> q = em.createQuery("SELECT o FROM Document AS o WHERE o.conference = :conference", Document.class);
	        q.setParameter("conference", conference);
	        return q;
        }
    }
    
}
